<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	/**
	 * @author Garry Willits
	 * @copyright 
	 * @date 10th May 2011
	 */
class EditableTextBlock  extends ContentDao{
	const POPUPWidth=650;
	
	const POPUPHeight=570;
	/**
	 * 
	 * set the content id
	 * @param unknown_type $contentId
	 */
	public function contentId( $contentId ){
		$this->content_id = $contentId;
	}
	//TODO - Insert your code here
	/***
	 * The root controller
	 */
	protected $controller;
	/***
	 * The model for loading image data
	 */
	protected $model;

	protected $configOptions = Array(
		"numRows" => 20,
		"numCols" => 170
	);
	/**
	 * 
	 * Constructor
	 * @param unknown_type $action
	 */
	public function __construct( $action = FALSE,$config=null ) {
		parent::__construct( $action );
		$this->controller = CI_Controller::get_instance();
		$this->configOptions["action"] = current_url();
		$this->model = $this->controller->load->model( 'GalleryModel','gallerymodel' );
		if( is_array($config)){
			$this->configOptions = array_merge( $this->configOptions,$config  );
		}
		if( Request::prop("content") != null ){
			$this->content = $this->removeDodgyHTML(Request::prop("content"));
		}
		//TODO - Insert your code here
	}

	/**
	 *
	 * @see ContentDao::GetEditorDisplay()
	 */
	public  function Editor() {
		$categories = $this->controller->gallerymodel->getCategories();
		foreach ( $categories as $category ){
			$imgNum = $this->controller->gallerymodel->countImagesInCategory( $category->id );
			$category->imgs = $this->controller->gallerymodel->getImages( $category->id,$imgNum,0 );
		}
		$display = new Display();
		$display->content = $this->content;
		$display->SitePath =  base_url();
		$display->popupWidth = self::POPUPWidth;
		$display->popupHeight = self::POPUPHeight;
		$display->categories = $categories;
		$display->numCols = $this->configOptions[ "numCols" ];
		$display->numRows = $this->configOptions[ "numRows" ];
		$display->scripts = '../tinymce/jscripts/tiny_mce/tiny_mce_src.js';
		$display->styles = 'tinymce_page';
		$display->action = current_url();
		$display->base = "ContentDaoTemplates/tinymce";
		$display->finalize();
		return $display;
		//TODO - Insert your code here
	}
	/**
	 * (non-PHPdoc)
	 * @see ContentDao::GetEditorDisplay()
	 */
	public function GetEditorDisplay(){
		throw new Exception("Not implemented method", 500);
	}
	
	/**
	 * 
	 * Utility function to remove dodgy html
	 * @param string $str
	 * @return string
	 */
	protected function removeDodgyHTML( $str ){
		$allowedTags='<p><strong><em><u><h1><h2><h3><h4><h5><h6><img><table><tr><td>';
		$allowedTags.='<a><li><ol><ul><span><div><br><ins><del>';
		$input = str_replace("&nbsp;", "<span class=\"spacer\"> </span>", $str); 
		return strip_tags(stripslashes($input),$allowedTags);
	}
}


?>